package com.fsts.client;

import java.io.File;

import javax.swing.AbstractListModel;
import javax.swing.JFileChooser;

import com.fsts.util.Constant;


/**
 * 
 * 
 * 
 * @version 1.0
 * @since JDK1.6(建议)
 */
public class UserCenterGui extends javax.swing.JFrame {

	private static final long serialVersionUID = 1233499827664121936L;
	/** 接收文件按钮 */
	private javax.swing.JButton recieveFileButton;
	/** 关闭按钮 */
	private javax.swing.JButton closeButton;
	/** 发送按钮 */
	private javax.swing.JButton sendFileButton;
	/** 信息提示区域 */
	private javax.swing.JTextArea infoTextArea;
	
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JScrollPane jScrollPane3;
	private javax.swing.JLabel onlineUserLabel;
	private javax.swing.JList onlineUserList;
	/** 文件选择对话框 */
	private javax.swing.JFileChooser fileChooser;
	public UserCenterGui(String loginUserName){
		
//		initComponents(loginUserName);
//		new ValidateIdentityListener(infoTextArea,Constant.BOB_RECEIVE_PORT,recieveFileButton).start();	// 	启动BOB
		
		initComponents("Alice登录");
		new ValidateIdentityListener(infoTextArea,Constant.ALICE_RECEIVE_PORT,recieveFileButton).start();	// 	启动ALICE
		
	
	}
	@SuppressWarnings("serial")
	private void initComponents(String loginUserName) {
		sendFileButton = new javax.swing.JButton();
		jScrollPane1 = new javax.swing.JScrollPane();
		infoTextArea = new javax.swing.JTextArea();
		jScrollPane3 = new javax.swing.JScrollPane();
		onlineUserList = new javax.swing.JList();
		onlineUserLabel = new javax.swing.JLabel();
		closeButton = new javax.swing.JButton();
		jLabel2 = new javax.swing.JLabel();
		jLabel3 = new javax.swing.JLabel();
		recieveFileButton = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application
				.getInstance(com.fsts.client.Main.class).getContext()
				.getResourceMap(UserCenterGui.class);
		setTitle("客户端----"+loginUserName); // NOI18N
		setName("Form"); // NOI18N
		setResizable(false);

		sendFileButton.setText(resourceMap.getString("sendFileButton.text")); // NOI18N
		sendFileButton.setName("sendFileButton"); // NOI18N
		sendFileButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				sendFileButtonActionPerformed(evt);// 注册监听
			}
		});

		jScrollPane1.setName("jScrollPane1"); // NOI18N

		infoTextArea.setColumns(20);
		infoTextArea.setRows(5);
		infoTextArea.setText(resourceMap.getString("infoTextArea.text")); // NOI18N
		infoTextArea.setName("infoTextArea"); // NOI18N
		jScrollPane1.setViewportView(infoTextArea);

		jScrollPane3.setName("jScrollPane3"); // NOI18N

		onlineUserList.setBackground(resourceMap
				.getColor("onlineUserList.background")); // NOI18N
		onlineUserList.setModel(new AbstractListModel() {
			String[] strings = { "Alice", "Bob" };

			public int getSize() {
				return strings.length;
			}

			public Object getElementAt(int i) {
				return strings[i];
			}
		});
		onlineUserList.setName("onlineUserList"); // NOI18N
		jScrollPane3.setViewportView(onlineUserList);

		onlineUserLabel.setFont(resourceMap.getFont("onlineUserLabel.font")); // NOI18N
		onlineUserLabel.setText(resourceMap.getString("onlineUserLabel.text")); // NOI18N
		onlineUserLabel.setBorder(javax.swing.BorderFactory
				.createEtchedBorder());
		onlineUserLabel.setName("onlineUserLabel"); // NOI18N

		closeButton.setText(resourceMap.getString("closeButton.text")); // NOI18N
		closeButton.setName("closeButton"); // NOI18N
		closeButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				closeButtonActionPerformed(evt);
			}
		});

		jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
		jLabel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
		jLabel2.setName("jLabel2"); // NOI18N

		jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
		jLabel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
		jLabel3.setName("jLabel3"); // NOI18N

		recieveFileButton.setText(resourceMap
				.getString("recieveFileButton.text")); // NOI18N
		recieveFileButton.setName("recieveFileButton"); // NOI18N
		recieveFileButton.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						recieveFileButtonActionPerformed(evt);
						System.out.println("这是内部监听监");
					}
				});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addComponent(
																				jScrollPane1,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				324,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addGap(
																				18,
																				18,
																				18)
																		.addGroup(
																				layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addComponent(
																								jScrollPane3,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								151,
																								Short.MAX_VALUE)
																						.addComponent(
																								jLabel2,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								151,
																								Short.MAX_VALUE)
																						.addComponent(
																								jLabel3,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								151,
																								Short.MAX_VALUE)))
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addComponent(
																				recieveFileButton,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				93,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addGap(
																				18,
																				18,
																				18)
																		.addComponent(
																				sendFileButton,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				94,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addGap(
																				18,
																				18,
																				18)
																		.addComponent(
																				closeButton,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				99,
																				Short.MAX_VALUE)
																		.addGap(
																				18,
																				18,
																				18)
																		.addComponent(
																				onlineUserLabel,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				153,
																				javax.swing.GroupLayout.PREFERRED_SIZE)))
										.addContainerGap()));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																layout
																		.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.BASELINE)
																		.addComponent(
																				closeButton,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				39,
																				Short.MAX_VALUE)
																		.addComponent(
																				onlineUserLabel,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				39,
																				javax.swing.GroupLayout.PREFERRED_SIZE))
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addGap(
																				1,
																				1,
																				1)
																		.addGroup(
																				layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.BASELINE)
																						.addComponent(
																								recieveFileButton,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								40,
																								javax.swing.GroupLayout.PREFERRED_SIZE)
																						.addComponent(
																								sendFileButton,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								40,
																								javax.swing.GroupLayout.PREFERRED_SIZE))))
										.addGap(18, 18, 18)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																javax.swing.GroupLayout.Alignment.TRAILING,
																layout
																		.createSequentialGroup()
																		.addComponent(
																				jLabel2,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				27,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addGap(
																				10,
																				10,
																				10)
																		.addComponent(
																				jLabel3,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				102,
																				javax.swing.GroupLayout.PREFERRED_SIZE))
														.addComponent(
																jScrollPane1,
																javax.swing.GroupLayout.Alignment.TRAILING,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																284,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
																		.addComponent(
																				jScrollPane3,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				140,
																				javax.swing.GroupLayout.PREFERRED_SIZE)))
										.addContainerGap()));

		pack();
	}

	/**
	 * "发送文件"按钮
	 * (中间那个按钮)
	 * @param evt
	 */
	private void sendFileButtonActionPerformed(java.awt.event.ActionEvent evt) {
		fileChooser = new JFileChooser("请选择需要发送的文件");
		int returnVal = fileChooser.showOpenDialog(this);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			File file = fileChooser.getSelectedFile();
			infoTextArea.append("你已经成功登录+\n");
			// 在实时消息框中显示选中的文件路径
			// 需要往界面上输出信息时，就调用 infoTextArea.append(info)
			// 每一步都需要在infoTextArea输出信息,一般地，信息就是下列步骤中的发送内容和回复内容
			infoTextArea.append("请求发送文件 "+file.getAbsolutePath()+" 至Bob，\n正在等待对方确认接收\n");
		
			// 1)启动一个线程，在指定的端口和接收方 通信
			new ValedateIdentity("127.0.0.1", 20000, infoTextArea, file).start();
			
			// 3)A向B证明身份
			// A选择一个随机整数x，计算t= g^x (mod n)，然后将公钥PA、身份标识IDA和t 发送给B，然后等待回复;
			
			// A收到随机整数C后，计算y=x+SA×c，并发送y给B，然后等待回复;
			
			// A收到“身份确认”，则继续下一步，否则若收到“无法确认身份”，则跳至第3步
			
			// 4)A请求B证明身份
			// A发送“验证身份”信息给B，然后等待回复;
			
			// A收到B的公钥PB、身份标识IDB和t 后，计算v= (PB^e+IDB) mod n,
			// 然后选择一个随机整数c,将C发送给B，然后等待回复;
			
			// A收到y后，检验等式g^y×v^c≡t ( mod n )是否成立，若成立则接受B，发送“确认身份”给B， 否则跳至第4步
			
			// A收到“可以发送文件”请求，则继续下一步
			
			// 5)A对名文产生摘要，对摘要进行签名，计算共享密钥K，对明文加密
			// A对明文M计算哈希值，得到摘要D，D=SHA256(M);
			
			// A对摘要D签名，得到Sign, Sign=RSA.ENCRYPT(D,sa);			
			
			// A计算共享密钥K=(PB^e+IDB)^SA mod n，对明文M加密，得到密文C，C=DES.ENCRYPT(M,K);
			
			// A将C和Sign连接后得到F，F=C+Sign
			
			// 6)A将F发送给B；
			
			// 7)A收到“文件接收成功”，则继续下一步；否则若收到“接收文件有误，请重发”,则跳至第6步
			
			// 8)A关闭输入流，输出流，线程
			
		} else {
			// ...
		}
	}

	/**
	 * 关闭按钮
	 * 
	 * @param evt
	 */
	private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {
		System.exit(0);
	}

	/**
	 * "接收文件"按钮 <br>
	 * 单击该按钮会弹出文件选择框<br>
	 * 
	 * 
	 * @param evt
	 */
	private void recieveFileButtonActionPerformed(java.awt.event.ActionEvent evt) {
		
	}

}
